﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.Register
{
   public class SQLHelper
    {
       
        //数据库连接
        private readonly static string comm = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;

        #region 添加/修改/删除
        public static int Close(string sql, SqlParameter[] sqlp)
        {
            using (SqlConnection conn = new SqlConnection(comm))
            {
                using (SqlCommand A = new SqlCommand(sql, conn))
                {
                    A.Parameters.AddRange(sqlp); 
                    conn.Open();
                    int i = A.ExecuteNonQuery();
                    return i;
                }
            }
        }
        #endregion

        #region 查询
        public static DataTable Query(string sql, SqlParameter[] b)
        {
            using (SqlConnection conn = new SqlConnection(comm))
            {
                using (SqlDataAdapter A = new SqlDataAdapter(sql, conn))
                {
                    if (b!=null)
                    {
                        A.SelectCommand.Parameters.AddRange(b);
                    }
                    DataTable ds = new DataTable();
                    A.Fill(ds);
                    return ds;
                }
            }
        }
        #endregion

        /// <summary>
        /// 下拉框
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static DataSet Box(string sql)
        {
            using (SqlConnection conn = new SqlConnection(comm))
            {
                using (SqlDataAdapter A = new SqlDataAdapter(sql, conn))
                {
                    DataSet ds = new DataSet();
                    A.Fill(ds);
                    return ds;
                }
            }
        }

        //事务
        public static bool UOWBorrow(string[] sql, SqlParameter[][] pars)
        {
            bool isOk = true;//设置返回值默认为true

            using (SqlConnection conn = new SqlConnection(comm))
            {
                conn.Open();
                using (SqlTransaction tx = conn.BeginTransaction())//启动事务
                {
                    using (SqlCommand cmd = new SqlCommand())//创建SqlCommand对象
                    {
                        try
                        {
                            cmd.Connection = conn;
                            cmd.Transaction = tx;//为命令指定事务

                            for (int i = 0; i < sql.Length; i++)
                            {
                                cmd.CommandText = sql[i];
                                cmd.Parameters.AddRange(pars[i]);
                                cmd.ExecuteNonQuery();
                            }

                            tx.Commit();//提交事务
                        }
                        catch (Exception e)
                        {
                            tx.Rollback();//数据回滚；回滚到最初的状态

                            isOk = false;//设置返回值为false
                        }
                    }

                }
            }
            return isOk;
        }
    }
}
